package com.whpost.service.service;

import com.whpost.service.Vo.JgkbItem;
import com.whpost.service.Vo.JgkbResp;
import com.whpost.service.dao.cwqf.JgkbMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.IntStream;

@Service
public class JgkbService {

    @Resource
    private JgkbMapper jgkbMapper;

    public JgkbResp query(String dimension, String period, LocalDate date){
        String rq = date.toString();
        String source = "jd";           // 寄递
        String source2 = "yw";          // 邮务

        // 1. 根据维度确定 jb 与 pcode
        int jb;
        String pcode = null;
        switch (dimension){
            case "city":   jb = 2; pcode = null; break;
            case "county": jb = 2; pcode = ""; break;
            case "branch": jb = 3; pcode = ""; break;
            default: throw new IllegalArgumentException("维度错误");
        }

        // 2. 查询寄递
        List<JgkbItem> jdList = jgkbMapper.selectBranchData(rq, source, pcode, jb);
        // 3. 查询邮务
        List<JgkbItem> ywList = jgkbMapper.selectBranchData(rq, source2, pcode, jb);

        // 4. 合并
        Map<String, JgkbItem> map = new HashMap<>();
        merge(jdList, map, true);
        merge(ywList, map, false);

        List<JgkbItem> list = new ArrayList<>(map.values());

        // 5. 计算合计
        JgkbItem total = new JgkbItem();
        list.forEach(i -> {
            total.setAllArrears(total.getAllArrears().add(i.getPostArrears()).add(i.getBizArrears()));
            total.setAllOverdue(total.getAllOverdue().add(i.getPostOverdue()).add(i.getBizOverdue()));
            total.setPostArrears(total.getPostArrears().add(i.getPostArrears()));
            total.setPostOverdue(total.getPostOverdue().add(i.getPostOverdue()));
            total.setBizArrears(total.getBizArrears().add(i.getBizArrears()));
            total.setBizOverdue(total.getBizOverdue().add(i.getBizOverdue()));
        });

        // 6. 排序
        list.sort(Comparator.comparing(JgkbItem::getAllArrears).reversed());
        IntStream.range(0, list.size()).forEach(i -> list.get(i).setRank(i + 1));

        JgkbResp resp = new JgkbResp();
        resp.setScope(period);
        resp.setDate(rq);
        resp.setList(list);
        resp.setTotal(total);
        return resp;
    }

    private void merge(List<JgkbItem> src, Map<String, JgkbItem> dst, boolean isPost){
        src.forEach(s -> {
            JgkbItem i = dst.computeIfAbsent(s.getCode(), k -> {
                JgkbItem it = new JgkbItem();
                it.setCode(s.getCode());
                it.setName(s.getName());
                return it;
            });
            if (isPost){
                i.setPostArrears(s.getPostArrears());
                i.setPostOverdue(s.getPostOverdue());
            } else {
                i.setBizArrears(s.getPostArrears());   // 邮务结果直接复用
                i.setBizOverdue(s.getPostOverdue());
            }
        });
    }
}